//
//  WmcDevNetworkParamsCell.swift
//  YPWatermarkCamera
//
//  Created by 周诗松 on 2022/6/24.
//

import UIKit

class WmcDevNetworkParamsCell: WmcBaseTableCell, UITextFieldDelegate {
    // Internal
    
    var inputClosure: ((String) -> Void)?

    override func makeUI() {
        super.makeUI()

        lineView.backgroundColor = .wmc_grayH19_F5F5F5

        contentView.addSubview(requiredLabel)
        contentView.addSubview(paramsLabel)
        contentView.addSubview(valueTextFiled)
        contentView.addSubview(descLabel)
        contentView.addSubview(lineView)

        requiredLabel.snp.makeConstraints { make in
            make.left.equalTo(6)
            make.centerY.equalToSuperview().offset(-7)
            make.width.equalTo(5)
        }
        
        paramsLabel.snp.makeConstraints { make in
            make.left.equalTo(requiredLabel.snp.right).offset(5)
            make.centerY.equalTo(requiredLabel)
            make.width.equalTo(70)
        }
        
        valueTextFiled.snp.makeConstraints { make in
            make.left.equalTo(paramsLabel.snp.right).offset(5)
            make.top.equalTo(10)
            make.bottom.equalTo(descLabel.snp.top).offset(-8)
            make.right.equalTo(-6)
        }
        
        descLabel.snp.makeConstraints { make in
            make.left.equalTo(6)
            make.right.equalTo(-6)
            make.bottom.equalTo(-8)
            make.height.equalTo(15)
        }
        
        lineView.snp.makeConstraints { make in
            make.left.equalTo(12)
            make.right.equalTo(-12)
            make.bottom.equalToSuperview()
            make.height.equalTo(1)
        }
    }

    func bindModel(_ model: WmcDevNetworkApiDetailVM.WmcParamsModel) {
        requiredLabel.isHidden = !model.isRequired
        paramsLabel.text = model.key
        valueTextFiled.text = model.value
        descLabel.text = "描述：\(model.desc)"
    }
    
    private lazy var requiredLabel: UILabel = {
        let label = UILabel()
        label.text = "*"
        label.font = 13.wmc_font
        label.textColor = .red
        label.textAlignment = .left
        return label
    }()
    
    private lazy var paramsLabel : UILabel = {
        let label = UILabel()
        label.font = 13.wmc_font
        label.textColor = .wmc_grayH5_606066
        label.textAlignment = .left
        label.minimumScaleFactor = 0.5
        label.adjustsFontSizeToFitWidth = true
        return label
    }()
    
    private lazy var valueTextFiled: UITextField = {
        let textFiled = UITextField()
        textFiled.delegate = self
        textFiled.borderStyle = .roundedRect
        textFiled.font = 13.wmc_font
        textFiled.textColor = .wmc_grayH5_606066
        textFiled.returnKeyType = .done
        return textFiled
    }()

    private lazy var descLabel: UILabel = {
        let label = UILabel()
        label.font = 13.wmc_font
        label.textColor = .wmc_grayH5_606066
        label.textAlignment = .left
        label.numberOfLines = 0
        label.minimumScaleFactor = 0.5
        label.adjustsFontSizeToFitWidth = true
        return label
    }()

    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
    }

    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    }

    func textFieldShouldReturn(_ textField: UITextField) -> Bool {
        inputClosure?(textField.text ?? "")
        return true
    }
    
    func textFieldDidEndEditing(_ textField: UITextField) {
        inputClosure?(textField.text ?? "")
        textField.resignFirstResponder()
    }
}

